package apt.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.HibernateException;

import apt.bean.CategoryBean;
import apt.bean.DVDBean;
import apt.bean.ProducerBean;
import apt.bean.SupplierBean;
import apt.dao.CategoryDao;
import apt.dao.DVDSDao;
import apt.dao.ProducerDao;
import apt.dao.SupplierDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DVDAction extends ActionSupport implements ModelDriven<DVDBean>,
		ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<DVDBean> listDvdBeans;
	private DVDSDao dvdsDao;
	private DVDBean dvdBean;
	private String dvdId;
	private List<ProducerBean> listProducer;
	private List<CategoryBean> listCategory;
	private List<SupplierBean> listSupplier;
	private File userImage;
	private HttpServletRequest servletRequest;
	String userImageFileName = "";

	Random rand = new Random();
	static int num = 100;
	int randnum = rand.nextInt(num + 1);

	public DVDAction() {
		dvdsDao = new DVDSDao();
	}

	public String removeSpaces(String s) {
		StringTokenizer st = new StringTokenizer(s, " ", false);
		String t = "";
		while (st.hasMoreElements())
			t += st.nextElement();
		return t;
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}

	@Override
	public DVDBean getModel() {

		if (getDvdBean() != null) {
			dvdBean.setImage(userImageFileName);
		}
		return getDvdBean();

	}

	public String addDVD() {
		try {
			ServletContext context = ServletActionContext.getServletContext();
			String path = context.getRealPath("/uploadedImage/");
			System.out.println("Server path:" + path);
			Random random = new Random(9000);
			System.out.println(random);

			userImageFileName = +randnum + removeSpaces(userImageFileName);
			File fileToCreate = new File(path, userImageFileName);

			FileUtils.copyFile(this.userImage, fileToCreate);
			if (!dvdsDao.addDVD(getModel())) {
				return ERROR;
			}
		} catch (IOException e) {
			e.printStackTrace();
			e.toString();
			addActionError(e.getMessage());
			System.out.println("Returning.....................");
			return INPUT;
		}

		System.out.println(userImage);

		return SUCCESS;
	}

	public String initData() {
		try {
			listProducer = new ProducerDao().getListProcer();
			listCategory = new CategoryDao().getCategory();
			listSupplier = new SupplierDao().getListSupplier();
		} catch (Exception e) {
			return ERROR;
		}

		return SUCCESS;
	}

	public String fillDataDVD() {
		try {
			dvdBean = dvdsDao.getDVDByID(dvdId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public String deleteDVD() {
		try {
			dvdsDao.deleteDVD(dvdBean);
		} catch (Exception e) {
			return ERROR;
		}

		return SUCCESS;
	}

	public String getListAllDVD() {
		try {
			listDvdBeans = dvdsDao.getListDVDS();

		} catch (HibernateException ex) {
			ex.printStackTrace();
			return ERROR;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public List<DVDBean> getListDvdBeans() {
		return listDvdBeans;
	}

	public void setListDvdBeans(List<DVDBean> listDvdBeans) {
		this.listDvdBeans = listDvdBeans;
	}

	public DVDBean getDvdBean() {
		return dvdBean;
	}

	public void setDvdBean(DVDBean dvdBean) {
		this.dvdBean = dvdBean;
	}

	public String getDvdId() {
		return dvdId;
	}

	public void setDvdId(String dvdId) {
		this.dvdId = dvdId;
	}

	public DVDSDao getDvdsDao() {
		return dvdsDao;
	}

	public void setDvdsDao(DVDSDao dvdsDao) {
		this.dvdsDao = dvdsDao;
	}

	public List<ProducerBean> getListProducer() {
		return listProducer;
	}

	public void setListProducer(List<ProducerBean> listProducer) {
		this.listProducer = listProducer;
	}

	public List<CategoryBean> getListCategory() {
		return listCategory;
	}

	public void setListCategory(List<CategoryBean> listCategory) {
		this.listCategory = listCategory;
	}

	public List<SupplierBean> getListSupplier() {
		return listSupplier;
	}

	public void setListSupplier(List<SupplierBean> listSupplier) {
		this.listSupplier = listSupplier;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

}
